package com.wxboot.game.eliminate.modules.service;

import com.wxboot.game.eliminate.base.JellyCell;
import com.wxboot.game.eliminate.modules.repository.JellyEliminateRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class JellyEliminateInitService {

    @Autowired
    private JellyEliminateRepository jellyEliminateRepository;

    public void initJellyMatrixLevel(List<JellyCell[][]> jellyMatrixList) {
        List<String> matrixList = new ArrayList<>();
        jellyMatrixList.forEach(jellyMatrix -> {
            matrixList.add(this.getMatrix(jellyMatrix));
        });

        jellyEliminateRepository.initAllGameLevelMatrix(matrixList);
    }

    private String getMatrix(JellyCell[][] jellyMatrix) {
        StringBuilder matrix = new StringBuilder();
        for (int i = 0; i < jellyMatrix.length; i++) {
            for (int j = 0; j < jellyMatrix[i].length; j++) {
                matrix.append(jellyMatrix[i][j]);
            }

            if (i < jellyMatrix.length - 1) {
                matrix.append("\n");
            }
        }

        return matrix.toString();
    }
}
